﻿; (function ($) {
    var methods = {
        init: function (value) {
            var $this = $(this);
            if (value) {
                $this.val(value);
            }
            else {
                value = "北京市 北京市 东城区";
            }

            var areasKey = ['Provinces', 'Cities', 'Districts'];
            var wg = [], wheels = [];
            var arr = value.split(' ');
            for (var i = 0; i < areasKey.length; i++) {
                genWheels(areasKey[i], arr, i);
            }

            function genWheels(key, arr, index, isRet) {
                var keys = [], area;
                if (key === "Provinces") {
                    area = area_array;
                }
                if (key === "Cities") {
                    area = sub_array[arr[index - 1]];
                }
                if (key === "Districts") {
                    area = sub_arr[arr[index - 1]];
                }
                for (var e = 0; e < area.length; e++) {
                    if (area[e] == undefined) {
                        continue;
                    }
                    keys.push(area[e]);
                }
                if (isRet) {
                    return {
                        values: keys,
                        keys: keys,
                        label: areasKey[index]
                    };
                }
                wg.push({
                    values: keys,
                    keys: keys,
                    label: areasKey[index]
                });
            }
            wheels.push(wg);

            var display = "bottom", model = "scroller";
            if ($(window).width() >= 768) {
                display = "modal";
            }
            var lang = "en";
            if (window.LangType != undefined) {
                if (window.LangType == "CN") {
                    lang = "zh";
                } else {
                    lang = "en";
                }
            }
            $this.mobiscroll().scroller({
                theme: "bootstrap",
                lang: lang,
                display: display,
                mode: model,
                wheels: wheels,
                showLabel: false,
                rows: 6,//滚动区域内的行数  
                fixedWidth: [160, 160, 160],//三组滚动框的宽度  
                onBeforeShow: function (inst) {
                    if ($(this).attr("disabled")) {
                        return false;
                    }
                },
                onSelect: function (v, inst) {
                    if ($.isFunction(inst.settings.confirm)) {
                        inst.settings.confirm(v, $(this));
                    }
                    var addressSelector = $(this);
                    if (addressSelector.attr("confirm") != "" && addressSelector.attr("confirm") != undefined) {
                        var SelectorConfirm = null;
                        eval("SelectorConfirm=" + addressSelector.attr("confirm"));
                        if ($.isFunction(SelectorConfirm)) {
                            var arr = $(this).val().split(' ');
                            arr.push(sub_sort[arr[0] + arr[1] + arr[2]]);
                            SelectorConfirm(arr, addressSelector);
                        }
                    }
                },
                validate: function (html, index, time, dir, inst) {
                    if (parseInt(index) >= 0) {
                        val = inst._tempWheelArray;
                        var data, idx = parseInt(index) + 1//(1:市， 2:区);
                        //TODO:要改变的省市区类型
                        for (var e = idx; e < areasKey.length; e++) {
                            data = genWheels(areasKey[e], val, e, true);
                            val[e] = inst._tempWheelArray[e] = data.keys[0] + "";
                            wheels[0][e] = data;
                        }
                        //设置要改变的省市区数据
                        inst.settings.wheels = wheels;
                        //切换改变动作
                        inst.changeWheel([idx, idx + 1], 0);
                    }
                }
            });
        },
        getValue: function () {
            var arr = $(this).val().split(' ');
            return arr;
        },
        disabled: function () {
            if ($(this).length == 0) {
                return;
            }
            $(this).each(function () {
                $(this).parents("td").text($(this).val());
            })
            return $(this);
        }
    };
    $.fn.WmpSelectAddress = function (method) {
        if (window != window.parent && typeof (window.parent.$("#").WmpSelectAddress) == "function") {
            if (arguments.length > 1)
            {
              //  delete arguments[0];
                return window.parent.$(this).WmpSelectAddress(method, arguments[1]);
            }
            else {
                return window.parent.$(this).WmpSelectAddress(method);
            }
        } else {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }
        }
    };
})($);